package com.base.util;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanRecord;
import android.bluetooth.le.ScanResult;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.os.ParcelUuid;
import android.util.Log;

import androidx.annotation.NonNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class BHelper {
    private static final String TAG = "BHelper";
    private static BHelper instance;
    private Context context;
    private boolean mScanning = false;
    // 蓝牙开关/连接接收器
    private BroadcastReceiver bOperationReceiver;
    private boolean bOperationRegistered = false;
    // 蓝牙扫描接收器
    private BroadcastReceiver bScanReceiver;
    private boolean bScanRegistered = false;
    private Map<String, BOperationCallback> bOperationCallbackMap;

    private BHelper(Context context) {
        this.context = context.getApplicationContext();
    }

    public static BHelper getInstance(Context context) {
        if (instance == null) {
            synchronized (BHelper.class) {
                if (instance == null) {
                    instance = new BHelper(context);
                }
            }
        }
        return instance;
    }

    // 根据id注销指定的监听器
    public void unRegisterBOperationReceiver(String id) {
        try {
            if (bOperationCallbackMap != null && !bOperationCallbackMap.containsKey(id)) {
                bOperationCallbackMap.remove(id);
            }
            // 当前没有任何运行中的监听器时，才需要注销广播接收器
            if (bOperationCallbackMap.isEmpty()) {
                if (bOperationReceiver != null && bOperationRegistered) {
                    context.unregisterReceiver(bOperationReceiver);
                    bOperationRegistered = false;
                    bOperationReceiver = null;
                }
            }
        } catch (Throwable t) {
            Log.d(TAG, t.getMessage() + "", t);
        }
    }

    /**
     * 注册广播接收器，用于接收蓝牙相关操作的结果
     * 参数中增加id，目的是支持同时注册多个监听器，否则后注册的监听器会覆盖前面的监听器，导致同一时间只能有一个地方使用蓝牙工具类
     */
    public void registerBOperationReceiver(String id, final BOperationCallback bOperationCallback) {
        if (bOperationCallback != null) {
            if (bOperationCallbackMap == null) {
                bOperationCallbackMap = new HashMap<>();
            }
            bOperationCallbackMap.put(id, bOperationCallback);

            if (bOperationReceiver == null) {
                try {
                    bOperationReceiver = new BroadcastReceiver() {
                        @Override
                        public void onReceive(Context context, Intent intent) {
                            try {
                                String action = intent.getAction();
                                // 蓝牙开关状态变化
                                if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
                                    //获取蓝牙广播中的蓝牙新状态
                                    int bNewState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);
                                    //获取蓝牙广播中的蓝牙旧状态
                                    int bOldState = intent.getIntExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, 0);
                                    switch (bNewState) {
                                        //正在打开蓝牙
                                        case BluetoothAdapter.STATE_TURNING_ON: {
                                            // no need to monitor this action
                                            break;
                                        }
                                        //蓝牙已打开
                                        case BluetoothAdapter.STATE_ON: {
                                            if (bOperationCallbackMap != null && !bOperationCallbackMap.isEmpty()) {
                                                for (Map.Entry<String, BOperationCallback> entry : bOperationCallbackMap.entrySet()) {
                                                    BOperationCallback callback = entry.getValue();
                                                    if (callback != null) {
                                                        callback.onEnabled();
                                                    }
                                                }
                                            }
                                            break;
                                        }
                                        //正在关闭蓝牙
                                        case BluetoothAdapter.STATE_TURNING_OFF: {
                                            // no need to monitor this action
                                            break;
                                        }
                                        //蓝牙已关闭
                                        case BluetoothAdapter.STATE_OFF: {
                                            if (bOperationCallbackMap != null && !bOperationCallbackMap.isEmpty()) {
                                                for (Map.Entry<String, BOperationCallback> entry : bOperationCallbackMap.entrySet()) {
                                                    BOperationCallback callback = entry.getValue();
                                                    if (callback != null) {
                                                        callback.onDisabled();
                                                    }
                                                }
                                            }
                                            break;
                                        }
                                    }
                                }
                                /*
                                 * 本机的蓝牙连接状态发生变化
                                 *
                                 * 特指“无任何连接”→“连接任意远程设备”，以及“连接任一或多个远程设备”→“无任何连接”的状态变化，
                                 * 即“连接第一个远程设备”与“断开最后一个远程设备”时才会触发该Action
                                 */
                                else if (action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {
                                    //获取蓝牙广播中的蓝牙连接新状态
                                    int newConnState = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, 0);
                                    //获取蓝牙广播中的蓝牙连接旧状态
                                    int oldConnState = intent.getIntExtra(BluetoothAdapter.EXTRA_PREVIOUS_CONNECTION_STATE, 0);
                                    // 当前远程蓝牙设备
                                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                                    HashMap<String, Object> map = parseDevice2Map(device);
                                    switch (newConnState) {
                                        //蓝牙连接中
                                        case BluetoothAdapter.STATE_CONNECTING: {
                                            // no need to monitor this action
                                            break;
                                        }
                                        //蓝牙已连接
                                        case BluetoothAdapter.STATE_CONNECTED: {
                                            if (bOperationCallbackMap != null && !bOperationCallbackMap.isEmpty()) {
                                                for (Map.Entry<String, BOperationCallback> entry : bOperationCallbackMap.entrySet()) {
                                                    BOperationCallback callback = entry.getValue();
                                                    if (callback != null) {
                                                        callback.onConnectionChanged(true, map);
                                                    }
                                                }
                                            }
                                            break;
                                        }
                                        //蓝牙断开连接中
                                        case BluetoothAdapter.STATE_DISCONNECTING: {
                                            // no need to monitor this action
                                            break;
                                        }
                                        //蓝牙已断开连接
                                        case BluetoothAdapter.STATE_DISCONNECTED: {
                                            if (bOperationCallbackMap != null && !bOperationCallbackMap.isEmpty()) {
                                                for (Map.Entry<String, BOperationCallback> entry : bOperationCallbackMap.entrySet()) {
                                                    BOperationCallback callback = entry.getValue();
                                                    if (callback != null) {
                                                        callback.onConnectionChanged(false, map);
                                                    }
                                                }
                                            }
                                            break;
                                        }
                                    }
                                }
                                // 有远程设备成功连接至本机
                                else if (action.equals(BluetoothDevice.ACTION_ACL_CONNECTED)) {
                                    // 当前远程蓝牙设备
                                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                                    HashMap<String, Object> map = parseDevice2Map(device);
                                    if (bOperationCallbackMap != null && !bOperationCallbackMap.isEmpty()) {
                                        for (Map.Entry<String, BOperationCallback> entry : bOperationCallbackMap.entrySet()) {
                                            BOperationCallback callback = entry.getValue();
                                            if (callback != null) {
                                                callback.onDeviceConnected(map);
                                            }
                                        }
                                    }
                                }
                                // 有远程设备断开连接（连接至一个蓝牙设备时，若关闭蓝牙，则只会触发STATE_DISCONNECTED，不会触发ACTION_ACL_DISCONNECTED）
                                else if (action.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)) {
                                    // 当前远程蓝牙设备
                                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                                    HashMap<String, Object> map = parseDevice2Map(device);
                                    if (bOperationCallbackMap != null && !bOperationCallbackMap.isEmpty()) {
                                        for (Map.Entry<String, BOperationCallback> entry : bOperationCallbackMap.entrySet()) {
                                            BOperationCallback callback = entry.getValue();
                                            if (callback != null) {
                                                callback.onDeviceDisconnected(map);
                                            }
                                        }
                                    }
                                }
                            } catch (Throwable t) {
                                Log.d(TAG, t.getMessage() + "", t);
                            }
                        }
                    };
                    IntentFilter filter = new IntentFilter();
                    // 蓝牙开关状态
                    filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
                    // 本机的蓝牙连接状态发生变化（连接第一个远程设备与断开最后一个远程设备才触发）
                    filter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
                    // 有远程设备成功连接至本机(每个远程设备都会触发)
                    filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
                    // 有远程设备断开连接(每个远程设备都会触发)
                    filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
                    context.registerReceiver(bOperationReceiver, filter);
                    bOperationRegistered = true;
                } catch (Throwable t) {
                    Log.d(TAG, t.getMessage() + "", t);
                }
            }
        }
    }

    /**
     * 打开蓝牙
     */
    @SuppressLint("MissingPermission")
    public void open() {
        /*try {
            if (DeviceHelper.getInstance(context).checkPermission("android.permission.BLUETOOTH")
                    && DeviceHelper.getInstance(context).checkPermission("android.permission.BLUETOOTH_ADMIN")) {
                //方式一：请求打开蓝牙
                //Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                //activity.startActivityForResult(intent, 1);
                //方式二：半静默打开蓝牙
                //低版本android会静默打开蓝牙，高版本android会请求打开蓝牙
                BluetoothAdapter adapter = getBAdapter();
                adapter.enable();
            }
        } catch (Throwable t) {
            Log.d(TAG, t.getMessage() + "", t);
        }*/
    }

    /**
     * 判断蓝牙是否已打开
     *
     * @return
     */
    @SuppressLint("MissingPermission")
    public boolean isEnabled() {
        boolean enabled = false;
       /* try {
            if (DeviceHelper.getInstance(context).checkPermission("android.permission.BLUETOOTH")) {
                BluetoothAdapter adapter = getBAdapter();
                if (adapter != null) {
                    //判断蓝牙是否开启
                    if (adapter.isEnabled()) {
                        enabled = true;
                    }
                } else {
                    // Device does not support Bluetooth
                }
            }
        } catch (Throwable t) {
            Log.d(TAG, t.getMessage() + "", t);
        }*/
        return enabled;
    }

    /**
     * 查询已配对的蓝牙设备
     */
    @SuppressLint("MissingPermission")
    public ArrayList<HashMap<String, Object>> getBondedDevice() {
        ArrayList<HashMap<String, Object>> result = new ArrayList<>();
        /*try {
            if (DeviceHelper.getInstance(context).checkPermission("android.permission.BLUETOOTH")) {
                BluetoothAdapter adapter = getBAdapter();
                Set<BluetoothDevice> pairedDevices = adapter.getBondedDevices();
                // If there are paired devices
                if (pairedDevices.size() > 0) {
                    for (BluetoothDevice device : pairedDevices) {
                        HashMap<String, Object> deviceInfo = parseDevice2Map(device);
                        deviceInfo.put("__currConnected", (isConnectedDevice(device) ? 1 : 0));
                        result.add(deviceInfo);
                    }
                }
            }
        } catch (Throwable t) {
            Log.d(TAG, t.getMessage() + "", t);
        }*/
        return result;
    }

    public boolean isConnectedDevice(BluetoothDevice device) {
        boolean isConnected = false;
        /*if (device != null) {
            try {
                if (DeviceHelper.getInstance(context).checkPermission("android.permission.BLUETOOTH")) {
                    //#if def{debuggable}
                    Boolean result = ReflectHelper.invokeInstanceMethod(device, "isConnected");
                    //#else
                    //#=Boolean result = ReflectHelper.invokeInstanceMethod(device, Strings.getString(115));
                    //#endif
                    if (result != null) {
                        isConnected = result.booleanValue();
                    }
                }
            } catch (Throwable t) {
                Log.d(TAG, t.getMessage() + "", t);
            }
        }*/
        return isConnected;
    }

    /**
     * 查找蓝牙，包括传统蓝牙和低功耗蓝牙
     * <p>
     * 注：
     * 1.该方式在查找低功耗蓝牙上效率较低
     * 2.若只需要查找低功耗蓝牙，应该使用“低功耗蓝牙API”，即 findLE() 方法
     * 3.为防止非正常终止扫描造成的内存泄漏，使用该方法后，需在适当的时机，主动调用一次unRegisterBtScanReceiver()，以注销接收器
     *
     * @param scanInterval  扫描时长，单位：秒，建议取值范围(0,12]
     * @param bScanCallback 扫描结果回调
     */
    @SuppressLint("MissingPermission")
    public void findLEAndClassic(int scanInterval, final BScanCallback bScanCallback) {
        /*try {
            if (DeviceHelper.getInstance(context).checkPermission("android.permission.BLUETOOTH")
                    && DeviceHelper.getInstance(context).checkPermission("android.permission.BLUETOOTH_ADMIN")) {
                final BluetoothAdapter adapter = getBAdapter();
                if (!adapter.isEnabled()) {
                    // 若蓝牙未打开，直接返回
                    bScanCallback.onScan(new ArrayList<>());
                    return;
                }
                if (mScanning) {
                    // 正在扫描中，直接返回
                    bScanCallback.onScan(new ArrayList<>());
                    return;
                }
                // 默认扫描6秒，若scanInterval不合法，则使用默认值
                final int defaultInterval = 6;
                if (scanInterval <= 0) {
                    scanInterval = defaultInterval;
                }

                // 通过bluetoothAdapter.startDiscovery()实现的扫描，系统会在扫描结束（通常是12秒）后自动停止，
                // 而cancelDiscovery()可以提前终止扫描。 所以这里的控制逻辑，相当于设置一个最大时间，限制扫描不得超出这个时间，
                // 但是很可能提前完成扫描（比如scanInterval > 12秒）
                // 设置一段时间后停止扫描（以防系统未正常停止扫描）
                final Handler handler = HandlerThread.newHandler((Handler.Callback) msg -> {
                    // 若已经停止扫描（系统扫描结束/通过cancelDiscovery取消扫描），则再次调用该方法不会触发ACTION_DISCOVERY_FINISHED
                    adapter.cancelDiscovery();
                    return false;
                });
                handler.sendEmptyMessageDelayed(0, scanInterval * 1000);

                // 准备开始扫描
                final ArrayList<HashMap<String, Object>> scanResult = new ArrayList<>();
                bScanReceiver = new BroadcastReceiver() {
                    public void onReceive(Context context, Intent intent) {
                        try {
                            String action = intent.getAction();

                            if (action.equals(BluetoothDevice.ACTION_FOUND)) { //found device
                                BluetoothDevice device = intent
                                        .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                                HashMap<String, Object> map = parseDevice2Map(device);
                                // 该extra取值与BluetoothDevice对象中getName()取值一致，因此不需要通过它获取name
//                          String name = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
                                short defaultValue = 0;
                                short rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, defaultValue);
                                map.put("rssi", rssi);
                                scanResult.add(map);
                            } else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_STARTED)) {
                                Log.d(TAG, "started");
                            } else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) {
                                Log.d(TAG, "done");
                                mScanning = false;
                                bScanCallback.onScan(scanResult);
                                // 若系统先扫描完，不需要再通过代码主动停止扫描
                                handler.removeMessages(0);
                                // 注销接收器
                                unRegisterBScanReceiver();
                            }
                        } catch (Throwable t) {
                            Log.d(TAG, t.getMessage() + "", t);
                        }
                    }
                };
                IntentFilter filter = new IntentFilter();
                // 用BroadcastReceiver来取得搜索结果
                filter.addAction(BluetoothDevice.ACTION_FOUND);
                filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
                // 两种情况会触发ACTION_DISCOVERY_FINISHED：1.系统结束扫描（约12秒）；2.调用cancelDiscovery()方法主动结束扫描
                filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
                context.registerReceiver(bScanReceiver, filter);
                bScanRegistered = true;

                // 开始扫描
                mScanning = true;
                adapter.startDiscovery();
            } else {
                // 缺少权限，直接返回
                bScanCallback.onScan(new ArrayList<>());
            }
        } catch (Throwable t) {
            Log.d(TAG, t.getMessage() + "", t);
            bScanCallback.onScan(new ArrayList<>());
        }*/
    }

    public void unRegisterBScanReceiver() {
        try {
            if (bScanReceiver != null && bScanRegistered) {
                context.unregisterReceiver(bScanReceiver);
                bScanRegistered = false;
                bScanReceiver = null;
            }
        } catch (Throwable t) {
            Log.d(TAG, t.getMessage() + "", t);
        }
    }

    /**
     * 查找低功耗蓝牙，该方法在4.3（API 18）以上，无法查找“传统蓝牙”
     *
     * @param scanInterval  扫描时长，单位：秒
     * @param adapter
     * @param bScanCallback 扫描结果回调
     */
    @SuppressLint("MissingPermission")
    public void findLE(int scanInterval, final BluetoothAdapter adapter, final BScanCallback bScanCallback) {
        /*try {
            if (DeviceHelper.getInstance(context).checkPermission("android.permission.BLUETOOTH")
                    && DeviceHelper.getInstance(context).checkPermission("android.permission.BLUETOOTH_ADMIN")) {
                if (!adapter.isEnabled()) {
                    // 若蓝牙未打开，直接返回
                    bScanCallback.onScan(new ArrayList<>());
                    return;
                }
                if (mScanning) {
                    // 正在扫描中，直接返回
                    bScanCallback.onScan(new ArrayList<>());
                    return;
                }
                // 默认扫描6秒，若scanInterval不合法，则使用默认值
                final int defaultInterval = 6;
                if (scanInterval <= 0) {
                    scanInterval = defaultInterval;
                }
                // 4.3的低功耗蓝牙API
                if (Build.VERSION.SDK_INT >= 18) {
                    final ArrayList<HashMap<String, Object>> scanResult = new ArrayList<>();
                    // 5.0又引入了新的蓝牙API(4.3版本的API仍然可用)
                    if (Build.VERSION.SDK_INT < 21) {
                        // 定义扫描结果回调
                        final BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() {
                            *//**
                             *
                             * @param device 扫描到的设备实例，可从实例中获取到相应的信息。如：名称，mac地址
                             * @param rssi 可理解成设备的信号值。该数值是一个负数，越大则信号越强
                             * @param scanRecord 远程设备提供的广播数据的内容
                             *//*
                            @Override
                            public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
                                try {
                                    HashMap<String, Object> map = parseDevice2Map(device);
                                    map.put("rssi", rssi);
//                                  map.put("scanRecord", Data.byteToHex(scanRecord));
                                    scanResult.add(map);
                                } catch (Throwable t) {
                                    Log.d(TAG, t.getMessage() + "", t);
                                }
                            }
                        };

                        // 开始扫描
                        mScanning = true;
                        adapter.startLeScan(leScanCallback);

                        // 设置一段时间后停止扫描
                        Handler handler = HandlerThread.newHandler((Handler.Callback) msg -> {
                            mScanning = false;
                            adapter.stopLeScan(leScanCallback);
                            bScanCallback.onScan(scanResult);
                            return false;
                        });
                        handler.sendEmptyMessageDelayed(0, scanInterval * 1000);
                    } else {
                        // 定义扫描结果回调
                        final ScanCallback mScanCallback = new ScanCallback() {
                            //当一个蓝牙ble广播被发现时回调
                            @Override
                            public void onScanResult(int callbackType, ScanResult result) {
                                super.onScanResult(callbackType, result);
                                //扫描类型有开始扫描时传入的ScanSettings相关
                                //对扫描到的设备进行操作。如：获取设备信息。
                                if (result != null) {
                                    HashMap<String, Object> map = new HashMap<>();
                                    BluetoothDevice device = result.getDevice();
                                    if (device != null) {
                                        map = parseDevice2Map(device);
                                    }
                                    map.put("rssi", result.getRssi());
                                    ScanRecord scanRecord = result.getScanRecord();
                                    scanResult.add(map);
                                }
                            }

                            // 批量返回扫描结果。一般蓝牙设备对象都是通过onScanResult(int,ScanResult)返回，
                            // 而不会在onBatchScanResults(List)方法中返回，除非手机支持批量扫描模式并且开启了批量扫描模式。
                            // 批处理的开启请查看ScanSettings。
                            //@param results 以前扫描到的扫描结果列表。
                            @Override
                            public void onBatchScanResults(List<ScanResult> results) {
                                super.onBatchScanResults(results);
                            }

                            //当扫描不能开启时回调
                            @Override
                            public void onScanFailed(int errorCode) {
                                super.onScanFailed(errorCode);
                                //扫描太频繁会返回ScanCallback.SCAN_FAILED_APPLICATION_REGISTRATION_FAILED，表示app无法注册，无法开始扫描。
                            }
                        };
                        //开始扫描
                        final BluetoothLeScanner mBLEScanner = adapter.getBluetoothLeScanner();
                        mScanning = true;
*//** 也可指定过滤条件和扫描配置
 //创建ScanSettings的build对象用于设置参数
 ScanSettings.Builder builder = new ScanSettings.Builder()
 //设置高功耗模式
 .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY);
 //android 6.0添加设置回调类型、匹配模式等
 if(android.os.Build.VERSION.SDK_INT >= 23) {
 //定义回调类型
 builder.setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES);
 //设置蓝牙LE扫描滤波器硬件匹配的匹配模式
 builder.setMatchMode(ScanSettings.MATCH_MODE_STICKY);
 }
 // 若设备支持批处理扫描，可以选择使用批处理，但此时扫描结果仅触发onBatchScanResults()
 //             if (bluetoothAdapter.isOffloadedScanBatchingSupported()) {
 //                 //设置蓝牙LE扫描的报告延迟的时间（以毫秒为单位）
 //                 //设置为0以立即通知结果
 //                 builder.setReportDelay(0L);
 //             }
 ScanSettings scanSettings = builder.build();
 //可设置过滤条件，在第一个参数传入，但一般不设置过滤。
 mBLEScanner.startScan(null, scanSettings, mScanCallback);
 *//*
                        mBLEScanner.startScan(mScanCallback);
                        // 设置一段时间后停止扫描
                        Handler handler = HandlerThread.newHandler((Handler.Callback) msg -> {
                            mScanning = false;
                            mBLEScanner.stopScan((mScanCallback));
                            bScanCallback.onScan(scanResult);
                            return false;
                        });
                        handler.sendEmptyMessageDelayed(0, scanInterval * 1000);
                    }
                } else {
                    findLEAndClassic(scanInterval, bScanCallback);
                }
            } else {
                // 缺少权限，直接返回
                bScanCallback.onScan(new ArrayList<>());
            }
        } catch (Throwable t) {
            Log.d(TAG, t.getMessage() + "", t);
            bScanCallback.onScan(new ArrayList<>());
        }*/
    }

    private BluetoothAdapter getBAdapter() {
        BluetoothAdapter adapter = null;
        /*try {
            if (Build.VERSION.SDK_INT >= 18) {
                BluetoothManager manager = (BluetoothManager) DeviceHelper.getInstance(context).getSystemServiceSafe(Context.BLUETOOTH_SERVICE);
                adapter = manager.getAdapter();

            } else {
                adapter = BluetoothAdapter.getDefaultAdapter();
            }
        } catch (Throwable t) {
            Log.d(TAG, t.getMessage() + "", t);
        }*/
        return adapter;
    }

    @SuppressLint("MissingPermission")
    private HashMap<String, Object> parseDevice2Map(BluetoothDevice device) {
        HashMap<String, Object> map = new HashMap<>();
        if (device != null) {
            try {
                map.put("name", device.getName());
                map.put("address", device.getAddress());
                map.put("bondState", device.getBondState());

                BluetoothClass btClass = device.getBluetoothClass();
                int majorClass = btClass.getMajorDeviceClass();
                int deviceClass = btClass.getDeviceClass();
                map.put("majorClass", majorClass);
                map.put("deviceClass", deviceClass);

                if (Build.VERSION.SDK_INT >= 18) {
                    map.put("type", device.getType());
                }
                // 已配对的设备，同时获取其uuids
                if (Build.VERSION.SDK_INT >= 15 && device.getBondState() == 12) {
                    ArrayList<String> uuids = new ArrayList<>();
                    ParcelUuid[] parcelUuids = device.getUuids();
                    if (parcelUuids != null && parcelUuids.length > 0) {
                        for (ParcelUuid parcelUuid : parcelUuids) {
                            if (parcelUuid != null && parcelUuid.getUuid() != null) {
                                uuids.add(parcelUuid.getUuid().toString());
                            }
                        }
                    }
                    map.put("uuids", uuids);
                }
            } catch (Throwable t) {
                Log.d(TAG, t.getMessage() + "", t);
            }
        }
        return map;
    }

    public static class BOperationCallback {
        /**
         * 打开蓝牙
         */
        protected void onEnabled() {
        }

        /**
         * 断开蓝牙
         */
        protected void onDisabled() {
        }

        /**
         * 蓝颜连接状态变化
         *
         * @param connect  true：连接到第一个设备，false：断开最后一个设备
         * @param btDevice 当前设备
         */
        protected void onConnectionChanged(boolean connect, HashMap<String, Object> btDevice) {
        }

        /**
         * 有远程设备成功连接
         *
         * @param btDevice
         */
        protected void onDeviceConnected(HashMap<String, Object> btDevice) {
        }

        /**
         * 有远程设备断开连接
         *
         * @param btDevice
         */
        protected void onDeviceDisconnected(HashMap<String, Object> btDevice) {
        }
    }

    public interface BScanCallback {
        void onScan(ArrayList<HashMap<String, Object>> result);
    }
}
